package com.khy.hyxx;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

public class LargeImageActivity extends Activity implements OnGestureListener, android.view.GestureDetector.OnGestureListener{  
    /**图片视图*/  
    private ImageView image;  
    /**图片所在的行号*/  
    private int id;  
    /**手势管理器*/  
    private GestureDetector gd;  
    private Cursor cursor;  
    /**图片的字节数组*/  
    private byte[] data;  
    /**图片名字集合*/  
    private ArrayList<String> list = new ArrayList();  
      
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.llook_image_browse_layout);  
        image = (ImageView) findViewById(R.id.imageView1);  
        //注册手势管理  
        gd = new GestureDetector(this);  
        //得到行号  
        id = getIntent().getIntExtra("id", id);  
        //得到传递过来的图片名字集合  
        list = getIntent().getStringArrayListExtra("list");  
        //调用设置图片方法  
        setImage();   
    }  
  
    /**  
     * 设置图片  
     */  
    public void setImage(){  
        //到SD卡查询图片信息  
        cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,  
                null,null,null,null);  
          
        while(cursor.moveToNext()){  
            String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));  
            byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA));  
            //查询到的图片名字和点击传递过来的图片名字相同时，设置图片  
            if(name.equals(list.get(id))){  
                Bitmap bitmap = BitmapFactory.decodeFile(new String(data,0,data.length-1));  
                image.setImageBitmap(bitmap);  
            }  
        }  
    }  
      
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        //把该事件交给手势管理器来处理  
        return gd.onTouchEvent(event);  
    }  
    @Override  
    public boolean onDown(MotionEvent e) {  
        return false;  
    }  
    /**  
     * 对手势操作后组件的处理  
     */  
    @Override  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        //从右往左滑动  
        if(e1.getX()-e2.getX()>50){  
            id++;  
            if(id<=cursor.getCount()){  
                setImage();  
            }else{  
                id=0;  
                setImage();  
            }  
        }  
        //从左往右滑动  
        if(e1.getX()-e2.getX()<50){  
            id--;  
            if(id>=0){  
                setImage();  
            }else{  
                id=cursor.getCount();  
                setImage();  
            }  
        }  
        return false;  
    }  
    @Override  
    public void onLongPress(MotionEvent e) {}  
    @Override  
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
            float distanceY) {  
        return false;  
    }  
    @Override  
    public void onShowPress(MotionEvent e) {}  
    @Override  
    public boolean onSingleTapUp(MotionEvent e) {  
        return false;  
    }

	@Override
	public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGesture(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
		// TODO Auto-generated method stub
		
	}  
  
}  
